/**
 * @file   main2.cpp
 * @author Shao <student@student>
 * @date   Tue Dec 28 22:11:14 2021
 * 
 * @brief  
 * 
 * 
 */


#include <iostream>
#include <ctime>
#define INF 1e9
using namespace std;

void print(int bak[], int k)
{
    if (bak[k] == k)
    {
        cout << "v" << k << " ";
        return;
    }
    print(bak, bak[k]);
    cout << "v" << k << " ";
    return;
}

int main()
{
    int i, j, n, m;
    int dis[99], bak[99], u[99], v[99], w[99];
    int check;
    time_t first,second;

    /*
     * 输入顶点数与边数。
     */
    cin >> n >> m;

    /*
     * 输入顶点1、顶点2及其对应边的权重。
     */
    first = clock();
    for (i = 1; i <= m; ++i)
    {
        cin >> u[i] >> v[i] >> w[i];
    }
    second  = clock();
    cout << "输入数据所需的时间为:" << (double)(second - first) / CLOCKS_PER_SEC << endl;

    /*
     * 初始化bak[]与dis[]数组。
     */
    first = clock();
    for (i = 1; i <= n; ++i)
    {
        bak[i] = i;
        dis[i] = INF;
    }
    dis[1] = 0;

    /*
     * Bellman-Ford算法的实现。
     */
    for (j = 1; j <= n-1; ++j)
    {
        /*
         * check用来标记本轮松弛中数组dis是否发生更新。
         */
        check = 0;
        for (i = 1; i <= m; ++i)
        {
            if (dis[u[i]] != INF && dis[u[i]] + w[i] < dis[v[i]])  // relax
            {
                dis[v[i]] = dis[u[i]] + w[i];
                bak[v[i]] = u[i];
                check = 1;
            }
        }

        if (check == 0)
        {
            break;
        }
    }
    /*
     * 输出结果。
     */
    cout << "以v1为起点的图的最短路径为:" << endl;
    for (i = 1; i <= n; ++i)
    {
        print(bak, i);
        cout << "=" << dis[i] << endl;
    }
    second  = clock();
    cout << "Bellman-Ford算法求最短路径所需的时间:" << (double)(second - first) / CLOCKS_PER_SEC << endl;
    return 0;
}
